package view

import (
	"github.com/flaravel/quanlaolao/pkg/flash"
	"github.com/flaravel/quanlaolao/pkg/logger"
	"github.com/flaravel/quanlaolao/pkg/route"
	"html/template"
	"io"
	"path/filepath"
	"strconv"
	"strings"
)

type D map[string]interface{}

// Render 渲染通用视图
func Render(w io.Writer, data D, tplFiles ...string) {
	RenderTemplate(w, "app", data, tplFiles...)
}

// RenderSimple 渲染简单的视图
func RenderSimple(w io.Writer, data D, tplFiles ...string) {
	RenderTemplate(w, "simple", data, tplFiles...)
}

// RenderTemplate 渲染视图
func RenderTemplate(w io.Writer, name string, data D, tplFiles ...string) {

	var err error
	data["flash"] = flash.All()

	allFiles := getTemplateFiles(tplFiles...)

	tmpl, err := template.New("").Funcs(
		template.FuncMap{
			"RouteName2URL": route.Name2URL,
			"Itoa": strconv.Itoa,
		}).ParseFiles(allFiles...)
	if err = tmpl.ExecuteTemplate(w, name, data); err != nil {
		logger.LogError(err)
	}
}

func getTemplateFiles(tplFiles ...string) []string {
	// 1 设置模板相对路径
	viewDir := "resources/views/"

	// 2. 遍历传参文件列表 Slice，设置正确的路径，支持 dir.filename 语法糖
	for i, f := range tplFiles {
		tplFiles[i] = viewDir + strings.Replace(f, ".", "/", -1) + ".gohtml"
	}
	// 3. 所有布局模板文件 Slice
	layoutFiles, err := filepath.Glob(viewDir + "layouts/*.gohtml")
	logger.LogError(err)
	// 4. 合并所有文件
	return append(layoutFiles, tplFiles...)
}
